import Vue from 'vue';
import Vuex from 'vuex';
import { ApiUrl } from '@/service/env.js';
import { personalCenterSync, PersonalCenter } from '@/service/user/user.service.js'

Vue.use(Vuex);

const store = new Vuex.Store({
  state: {
    /**
     * 是否需要强制登录
     */
    forcedLogin: false,
    hasLogin: false,
    userName: '',
    userInfo: {},
    imgBaseUrl: '/static/image',
    systemInfo: {
      windowHeight: null
    }
  },
  mutations: {
    login (state, userName) {
      state.userName = userName || '新用户';
      state.hasLogin = true;
    },
    logout (state) {
      state.userName = '';
      state.hasLogin = false;
    },
    // getUserInfo (state, params) { 
    //   state.userInfo = params;
    // },
    updateUserInfo (state, params) {
      state.userInfo = null;
      state.userInfo = params;
    },
    saveJoinInfo (state, params) {
      state.joinInfo = params;
    }
  },
  actions: {
    async getUserInfo({commit}) {
      return new Promise((resolve, reject) => {
        PersonalCenter({}, res => {
          if (res.status == 0) {
            commit('updateUserInfo', res.data);
            resolve(res.data);
          }
        });
      });
      // console.log('1');
      // const userinfo = await personalCenterSync({});
      // console.log('2');
      // commit('updateUserInfo', userinfo);
      // console.log('3');
    }
  }
});

export default store;
